//
//  confirmOrderViewController.m
//  lmq
//
//  Created by 千翼网络科技 on 2017/3/10.
//  Copyright © 2017年 yuanxin. All rights reserved.
//

#import "confirmOrderViewController.h"
#import "orderTableViewCell.h"
#import "addressViewController.h"
#import "UIImageView+WebCache.h"
#import "payControlViewController.h"
#import "configddd.h"
#import "discountCouponViewController.h"
#import "couponViewController.h"
#import "MJRefresh.h"
#import <Masonry/Masonry.h>
#import "loginViewController.h"

@interface confirmOrderViewController ()

@end

@implementation confirmOrderViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [self requestData];
    //下拉刷新
//    orderTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        
//        [self requestData];
//        [orderTableView.mj_header endRefreshing];
//        
//    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTitle:@"确认订单"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moneyNotification:) name:@"money" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addressIdNotification:) name:@"address_Id" object:nil];
    addressArr = [[NSMutableArray alloc]init];
    
//    收货地址
    
    UIView *xxView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, screenScale(160))];
    xxView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:xxView];
    
    UIButton *xButton=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, screenScale(150))];
    [xButton addTarget:self action:@selector(xButtonClick) forControlEvents:UIControlEventTouchUpInside];
    xButton.backgroundColor=[UIColor clearColor];
    [xxView addSubview:xButton];
    
    //收件人
    
    xixnLabel=[[UILabel alloc]init];
    xixnLabel.font = get4sFont(14);
    xixnLabel.textAlignment=NSTextAlignmentLeft;
    [xxView addSubview:xixnLabel];
    [xixnLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(xxView).with.offset(screenScale(28));
        make.left.mas_equalTo(xxView).with.offset(screenScale(20));
        
        make.height.mas_equalTo(screenScale(32));
    }];
    
    //电话
    
    xixnLabel1=[[UILabel alloc]init];
    xixnLabel1.font = get4sFont(14);
    xixnLabel1.textAlignment=NSTextAlignmentLeft;
    [xxView addSubview:xixnLabel1];
    [xixnLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(xxView).with.offset(screenScale(28));
        make.right.mas_equalTo(xxView).with.offset(screenScale(-150));
        
        make.height.mas_equalTo(screenScale(32));
    }];
    
    //收货地址
    
    xinLabel1=[[UILabel alloc]initWithFrame:CGRectMake((10), (26), DEF_SCREEN_WIDTH-(50), (65))];
    xinLabel1.font = get4sFont(13);
    xinLabel1.numberOfLines = 0;
    xinLabel1.textAlignment = NSTextAlignmentLeft;
    [xxView addSubview:xinLabel1];
    [xinLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(xxView).with.offset(screenScale(70));
        make.left.mas_equalTo(xxView).with.offset(screenScale(20));
        make.right.mas_equalTo(xxView).with.offset(screenScale(-80));
        
        make.height.mas_equalTo(screenScale(60));
    }];
    
    //线
    
    UIView *xianView=[[UIView alloc]initWithFrame:CGRectMake(0, screenScale(150), DEF_SCREEN_WIDTH, screenScale(10))];
    xianView.backgroundColor=[ZJCModel colorWithString:@"f2f2f2"];
    [xxView addSubview:xianView];
    
    //右箭头
    
    UIImageView *youImageView=[[UIImageView alloc]init];
    youImageView.image=[UIImage imageNamed:@"next"];
    [xxView addSubview:youImageView];
    [youImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(xxView).with.offset(screenScale(55));
        make.right.mas_equalTo(xxView).with.offset(screenScale(-20));
        make.width.mas_equalTo(screenScale(32));
        make.height.mas_equalTo(screenScale(32));
    }];
    
//    底部view
    
    UIView *bottomView=[[UIView alloc]init];
    [self.view addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view).with.offset(0);
        make.left.mas_equalTo(self.view).with.offset(0);
        make.width.mas_equalTo(scrWidth());
        make.height.mas_equalTo(screenScale(83));
    }];
    
    UIView *xView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, screenScale(1))];
    xView.backgroundColor=[ZJCModel colorWithString:@"cacaca"];
    [bottomView addSubview:xView];
    
    UIButton *payBtn=[[UIButton alloc]init];
    payBtn.backgroundColor=[UIColor blackColor];
    [payBtn setTitle:@"确认订单" forState:UIControlStateNormal];
    payBtn.titleLabel.font = get4sFont(14);
    [payBtn addTarget:self action:@selector(payBtn) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:payBtn];
    [payBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(bottomView).with.offset(0);
        make.right.mas_equalTo(bottomView).with.offset(0);
        make.width.mas_equalTo(screenScale(200));
        make.height.mas_equalTo(screenScale(83));
    }];
    
    UILabel *payLabel=[[UILabel alloc]init];
    payLabel.text=@"订单总金额：";
    payLabel.font = get4sFont(12);
    [bottomView addSubview:payLabel];
    [payLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(bottomView).with.offset(screenScale(30));
        make.left.mas_equalTo(bottomView).with.offset(screenScale(20));
        
        make.height.mas_equalTo(screenScale(30));
    }];
    
    zlabel = [[UILabel alloc]init];
    zlabel.textColor=[ZJCModel colorWithString:@"ff2222"];
    zlabel.textAlignment=NSTextAlignmentRight;
    zlabel.font = get4sFont(13);
    [bottomView addSubview:zlabel];
    [zlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(bottomView).with.offset(screenScale(30));
        make.left.mas_equalTo(bottomView).with.offset(screenScale(240));
        
        make.height.mas_equalTo(screenScale(30));
    }];
    
//    积分
    
    UIView *integralView = [[UIView alloc]initWithFrame:CGRectMake(0, DEF_SCREEN_HEIGHT-(50)-64-50, DEF_SCREEN_WIDTH, 50)];
    [self.view addSubview:integralView];
    [integralView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(bottomView).with.offset(screenScale(-83));
        make.left.mas_equalTo(self.view).with.offset(0);
        make.width.mas_equalTo(scrWidth());
        make.height.mas_equalTo(screenScale(83));
    }];
    
    UILabel *integralLable = [[UILabel alloc]init];
    integralLable.text = @"积    分：";
    integralLable.font = get4sFont(12);
    [integralView addSubview:integralLable];
    [integralLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(integralView).with.offset(screenScale(30));
        make.left.mas_equalTo(integralView).with.offset(screenScale(20));
        
        make.height.mas_equalTo(screenScale(30));
    }];
    
    integralNumLable = [[UILabel alloc]init];
    integralNumLable.font = get4sFont(12);
    [integralView addSubview:integralNumLable];
    [integralNumLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(integralView).with.offset(screenScale(30));
        make.left.mas_equalTo(integralView).with.offset(screenScale(240));
        
        make.height.mas_equalTo(screenScale(30));
    }];
    
    integralBtn = [[UIButton alloc]init];
    [integralBtn setImage:[UIImage imageNamed:@"circile_b"] forState:UIControlStateNormal];
    [integralBtn setImage:[UIImage imageNamed:@"duihao"] forState:UIControlStateSelected];
    [integralBtn addTarget:self action:@selector(integralBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [integralView addSubview:integralBtn];
    [integralBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(integralView).with.offset(screenScale(30));
        make.right.mas_equalTo(integralView).with.offset(screenScale(-30));
        make.width.mas_equalTo(screenScale(30));
        make.height.mas_equalTo(screenScale(30));
    }];
    
    UIView *threadView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, scrWidth(), screenScale(1))];
    threadView.backgroundColor = getColor(@"cacaca");
    [integralView addSubview:threadView];
    
//    优惠券
    
    UIView *discountCouponView = [[UIView alloc]init];
    [self.view addSubview:discountCouponView];
    [discountCouponView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(integralView).with.offset(screenScale(-83));
        make.left.mas_equalTo(self.view).with.offset(0);
        make.width.mas_equalTo(scrWidth());
        make.height.mas_equalTo(screenScale(83));
    }];
    
    UIButton *discountCouponBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, scrWidth(), screenScale(83))];
    [discountCouponBtn addTarget:self action:@selector(discountCouponBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [discountCouponView addSubview:discountCouponBtn];
    
    UILabel *discountCouponLable = [[UILabel alloc]init];
    discountCouponLable.text = @"优惠券:";
    discountCouponLable.font = get4sFont(12);
    [discountCouponView addSubview:discountCouponLable];
    [discountCouponLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(discountCouponView).with.offset(screenScale(30));
        make.left.mas_equalTo(discountCouponView).with.offset(screenScale(20));
        
        make.height.mas_equalTo(screenScale(30));
    }];
    
    discountNumLabel = [[UILabel alloc]init];
    discountNumLabel.font = get4sFont(12);
    [discountCouponView addSubview:discountNumLabel];
    [discountNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(discountCouponView).with.offset(screenScale(30));
        make.left.mas_equalTo(discountCouponView).with.offset(screenScale(240));
        
        make.height.mas_equalTo(screenScale(30));
    }];
    
    UIImageView *nextImg = [[UIImageView alloc]init];
    nextImg.image = [UIImage imageNamed:@"next"];
    [discountCouponView addSubview:nextImg];
    [nextImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(discountCouponView).with.offset(screenScale(30));
        make.right.mas_equalTo(discountCouponView).with.offset(screenScale(-27));
        make.width.mas_equalTo(screenScale(32));
        make.height.mas_equalTo(screenScale(32));
    }];
    
    UIView *threadView1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, scrWidth(), screenScale(1))];
    threadView1.backgroundColor = getColor(@"cacaca");
    [discountCouponView addSubview:threadView1];
    
    
    orderTableView=[[UITableView alloc]initWithFrame:CGRectMake(0,screenScale(160), DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT-64-screenScale(83*3)-screenScale(160)) style:UITableViewStyleGrouped];
    
    orderTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    //    orderTableView.backgroundColor=[UIColor purpleColor];
    orderTableView.dataSource=self;
    orderTableView.delegate=self;
    [self.view addSubview:orderTableView];
    
//    [self requestData];
}

#pragma mark - requestData

- (void)requestData{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    NSString *uidStr=[PublicFunction getConfigWithKey:@"uid"];
    NSString *tokenStr=[PublicFunction getConfigWithKey:@"token"];
    [productParams setValue:uidStr forKey:@"member_id"];
    [productParams setValue:tokenStr forKey:@"token"];
    
    [[MNetworkHandler sharedInstance]conURL:Order_Display
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       
                                       NSLog(@"=== return === %@",returnData);
                                       
                                       integralBtn.selected = NO;
                                       
                                       addressDic = [[NSMutableDictionary alloc]init];
                                       orderArr = [[NSMutableArray alloc]init];
                                       
                                       addressDic = [[returnData objectForKey:@"data"] objectForKey:@"address"];
                                       orderArr = [[returnData objectForKey:@"data"] objectForKey:@"shop"];
                                       integral_money = [[returnData objectForKey:@"data"] objectForKey:@"integral_money"];
                                       
                                       //收货地址变更
                                       if (addressArr.count > 0) {
                                           
                                           NSString *str = @"收件人：";
                                           xixnLabel.text = [str stringByAppendingString:[addressArr[0] objectForKey:@"consignee"]];
                                           
                                           xixnLabel1.text = [addressArr[0] objectForKey:@"phone"];
                                           
                                           NSString *str1 = @"收货地址：";
                                           NSString *str2 = [str1 stringByAppendingString:[addressArr[0] objectForKey:@"region"]];
                                           xinLabel1.text = [str2 stringByAppendingString:[addressArr[0] objectForKey:@"information"]];
                                       }else{
                                       
                                           NSString *str = @"收件人：";
                                           xixnLabel.text = [str stringByAppendingString:[addressDic objectForKey:@"consignee"]];
                                           
                                           xixnLabel1.text = [addressDic objectForKey:@"phone"];
                                           
                                           NSString *str1 = @"收货地址：";
                                           NSString *str2 = [str1 stringByAppendingString:[addressDic objectForKey:@"region"]];
                                           xinLabel1.text = [str2 stringByAppendingString:[addressDic objectForKey:@"information"]];
                                       }
                                       
                                       addressId = [addressDic[@"id"] intValue];
                                       
                                       if (![[returnData objectForKey:@"data"] objectForKey:@"integral_money"]) {
                                           
                                           integralNumLable.text = @"0";
                                       }else{
                                       
                                           integralNumLable.text = [NSString stringWithFormat:@"%.2f",[integral_money floatValue]];
                                       }
                                       
                                       
                                       
                                       all_money = 0;
                                       
                                       for (int i = 0; i < orderArr.count; i++) {
                                           
                                           for (int j = 0 ; j < [[orderArr[i]objectForKey:@"ordershop"]count]; j ++) {
                                               
                                               all_money = all_money + [[[orderArr[i] objectForKey:@"ordershop"][j] objectForKey:@"price"]floatValue] * [[[orderArr[i] objectForKey:@"ordershop"][j] objectForKey:@"num"]floatValue];
                                           }
                                       }
                                       
                                       [orderTableView reloadData];
                                       
                                       money = all_money;
                                       
                                       all_money = all_money - discountCoupon;
                                       
                                       if (all_money < discountCoupon) {
                                           
                                           all_money = 0;
                                       }
                                       
                                       zlabel.text = [NSString stringWithFormat:@"¥%.2f",all_money];
                                       //[self.view reloadInputViews];
                                       
                                       [hud removeFromSuperview];
                                       
                                   }
                                   else if ([[returnData objectForKey:@"code"]intValue]==-1){
                                       
                                       [hud removeFromSuperview];
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                       
                                   }else if ([[returnData objectForKey:@"code"]intValue] == -2){
                                   
                                       [hud removeFromSuperview];
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                       
                                       addressViewController *addressVC = [[addressViewController alloc]init];
                                       [self.navigationController pushViewController:addressVC animated:YES];
                                       
                                   }
                               } failureBlock:^(NSError *error) {
                                   
                                   [hud removeFromSuperview];
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
}

#pragma mark - notfication

- (void)moneyNotification:(NSNotification *)text{
    
    if ([text.userInfo[@"selected"] isEqualToString:@"no"]) {
        
        discountNumLabel.text = @"";
        discountCouponMark = @"no";
        discountCouponId = 0;
        
        discountCoupon = 0.0;
        
    }else{
        
        discountCouponMark = @"yes";
        discountNumLabel.text = text.userInfo[@"money"];
        discountCouponId = [text.userInfo[@"id"] intValue];
        integralBtn.selected = NO;
        
        discountCoupon = [text.userInfo[@"money"] floatValue];
    }
}

- (void)addressIdNotification:(NSNotification *)text{

    if ([text.userInfo[@"selected"] isEqualToString:@"yes"]) {
        
//        addressArr[0] = text.userInfo[@"addressId"];
        addressArr[0] = text.userInfo[@"address"];
    }
}

#pragma mark - click

- (void)discountCouponBtnClick{
    
    NSLog(@"优惠券");
    discountCouponViewController *discountCouponView = [[discountCouponViewController alloc]init];
    discountCouponView.mark = [NSString stringWithFormat:@"%d",discountCouponId];
    [self.navigationController pushViewController:discountCouponView animated:YES];
}

- (void)integralBtnClick{
    
    integralBtn.selected = !integralBtn.selected;
    
    if (integralBtn.selected) {
        
        if ([discountCouponMark isEqualToString:@"no"]) {
            
            integralMark = @"yes";
            discountCouponMark = @"no";
            discountNumLabel.text = @"";
            
            if ([integral_money floatValue] > all_money) {
                
                all_money = 0.0;
                zlabel.text = [NSString stringWithFormat:@"¥%.2f",0.0];
            }else{
                
                all_money -= [integral_money floatValue];
                zlabel.text = [NSString stringWithFormat:@"¥%.2f",all_money];
            }
            
        }else{
            
            all_money = money;
        
            integralMark = @"yes";
            discountCouponMark = @"no";
            discountNumLabel.text = @"";
            
            if ([integral_money floatValue] > all_money) {
                
                all_money = 0.0;
                zlabel.text = [NSString stringWithFormat:@"¥%.2f",0.0];
            }else{
                
                all_money -= [integral_money floatValue];
                zlabel.text = [NSString stringWithFormat:@"¥%.2f",all_money];
            }
        }
        
        
    }else{
        
        integralMark = @"no";
        
        if ([integral_money floatValue] > all_money) {
            
            for (int i = 0; i < orderArr.count; i++) {
                
                for (int j = 0 ; j < [[orderArr[i]objectForKey:@"ordershop"]count]; j ++) {
                    
                    all_money = all_money + [[[orderArr[i] objectForKey:@"ordershop"][j] objectForKey:@"price"]floatValue] * [[[orderArr[i] objectForKey:@"ordershop"][j] objectForKey:@"num"]floatValue];
                }
            }
            zlabel.text = [NSString stringWithFormat:@"¥%.2f",all_money];
        }else{
            
            all_money += [integral_money floatValue];
            zlabel.text = [NSString stringWithFormat:@"¥%.2f",all_money];
        }
    }
    
}

-(void)payBtn{
    NSLog(@"确认订单");
    
    // 登陆判断
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    [productParams setValue: [PublicFunction getConfigWithKey:@"token"] forKey:@"token"];
    
    [[MNetworkHandler sharedInstance]conURL:Is_Login
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       
                                       [self submitOrder];
                                       [hud removeFromSuperview];
                                   }else{
                                       
                                       [hud removeFromSuperview];
                                       loginViewController *loginVc =[[loginViewController alloc]init];
                                       [self.navigationController presentViewController:loginVc animated:YES completion:^{}];
                                   }
                               } failureBlock:^(NSError *error) {
                                   
                                   [hud removeFromSuperview];
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
}

- (void)submitOrder{

    NSDictionary *dict =[[NSDictionary alloc]initWithObjectsAndKeys:@"yes",@"select", nil];
    NSNotification *notification =[NSNotification notificationWithName:@"cart_num" object:nil userInfo:dict];
    [[NSNotificationCenter defaultCenter] postNotification:notification];
    
    if ([discountCouponMark isEqualToString:@"yes"]) {
        
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
        hud.tag = 500;
        //提示文本
        hud.labelText = @"正在加载...";
        //是否有遮罩
        hud.dimBackground = YES;
        
        //代金券
        NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
        NSString *uidStr=[PublicFunction getConfigWithKey:@"uid"];
        [productParams setValue:uidStr forKey:@"member_id"];
        [productParams setValue:@"1" forKey:@"integral"];
        [productParams setValue:[NSString stringWithFormat:@"%d",discountCouponId] forKey:@"vouchers_id"];
        
        if (addressArr.count < 1) {
            
            [productParams setValue:[NSString stringWithFormat:@"%d",addressId] forKey:@"address_id"];
        }else{
            
            [productParams setValue:[NSString stringWithFormat:@"%@",addressArr[0][@"id"]] forKey:@"address_id"];
        }
        NSLog(@"===product===%@",productParams);
        
        //生成订单-优惠券
        [[MNetworkHandler sharedInstance]conURL:Add_Vouchers_Order
                                    networkType:NetWorkPOST
                                         params:productParams
                                       delegate:nil
                                        showHUD:NO
                                   successBlock:^(id returnData) {
                                       if ([[returnData objectForKey:@"code"]intValue]==1) {
                                           
                                           [hud removeFromSuperview];
                                           
                                           [PublicFunction saveConfig:[returnData objectForKey:@"all_number"] forKey:@"all_number"];
                                           [PublicFunction saveConfig:[returnData objectForKey:@"order_id"] forKey:@"order_id"];
                                           
                                           payControlViewController *payControlView = [[payControlViewController alloc]init];
                                           payControlView.str = [NSString stringWithFormat:@"%.2f",all_money];
                                           [self.navigationController pushViewController:payControlView animated:YES];
                                           //                                           NSLog(@"=== return === %@",returnData);
                                           
                                       }
                                       else if ([[returnData objectForKey:@"code"]intValue]==-1){
                                           
                                           [hud removeFromSuperview];
                                           [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                           
                                       }
                                   } failureBlock:^(NSError *error) {
                                       
                                       [hud removeFromSuperview];
                                       NSLog(@"%@",error);
                                       [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                                   }];
    }else{
        
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
        hud.tag = 500;
        //提示文本
        hud.labelText = @"正在加载...";
        //是否有遮罩
        hud.dimBackground = YES;
        
        //积分
        NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
        NSString *uidStr=[PublicFunction getConfigWithKey:@"uid"];
        [productParams setValue:uidStr forKey:@"member_id"];
        
        if ([integralMark isEqualToString:@"yes"]) {
            
            [productParams setValue:@"1" forKey:@"integral"];
        }else{
            
            [productParams setValue:@"0" forKey:@"integral"];
        }
        
        if (addressArr.count < 1) {
            
            [productParams setValue:[NSString stringWithFormat:@"%d",addressId] forKey:@"address_id"];
        }else{
            
            [productParams setValue:[NSString stringWithFormat:@"%@",addressArr[0][@"id"]] forKey:@"address_id"];
        }
        NSLog(@"===product===%@",productParams);
        
        //生成订单-积分／不使用优惠券和积分
        [[MNetworkHandler sharedInstance]conURL:Add_Integral_Order
                                    networkType:NetWorkPOST
                                         params:productParams
                                       delegate:nil
                                        showHUD:NO
                                   successBlock:^(id returnData) {
                                       if ([[returnData objectForKey:@"code"]intValue]==1) {
                                           
                                           payControlViewController *payControlView = [[payControlViewController alloc]init];
                                           payControlView.str = [NSString stringWithFormat:@"%.2f",all_money];
                                           [PublicFunction saveConfig:[returnData objectForKey:@"all_number"] forKey:@"all_number"];
                                           [PublicFunction saveConfig:[returnData objectForKey:@"order_id"] forKey:@"order_id"];
                                           
                                           [hud removeFromSuperview];
                                           
                                           [self.navigationController pushViewController:payControlView animated:YES];
                                           //                                           NSLog(@"=== return === %@",returnData);
                                           
                                       }
                                       else if ([[returnData objectForKey:@"code"]intValue]==-1){
                                           
                                           [hud removeFromSuperview];
                                           [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                           
                                       }
                                   } failureBlock:^(NSError *error) {
                                       
                                       [hud removeFromSuperview];
                                       NSLog(@"%@",error);
                                       [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                                   }];
    }
}

#pragma mark -----UITableViewDataSource-----

//返回分区数(默认为1)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return orderArr.count;
}

//返回每一行的cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [[orderArr[section] objectForKey:@"ordershop"]count];
//    return 2;
}

//自定义section的头部
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *xView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, screenScale(68))];
    xView.backgroundColor=[UIColor whiteColor];
    
    UILabel *tittleLable = [[UILabel alloc]init];
    tittleLable.text = [NSString stringWithFormat:@"订单%ld",section + 1];
    tittleLable.font = get4sFont(14);
//    tittleLable.font = [UIFont boldSystemFontOfSize:14];
    [xView addSubview:tittleLable];
    [tittleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(xView).with.offset(0);
        make.left.mas_equalTo(xView).with.offset(screenScale(20));
        
        make.height.mas_equalTo(screenScale(68));
    }];
    
    UIView *threadView = [[UIView alloc]init];
    threadView.backgroundColor = [ZJCModel colorWithString:@"cacaca"];
    [xView addSubview:threadView];
    [threadView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(xView).with.offset(screenScale(-1));
        make.left.mas_equalTo(xView).with.offset(screenScale(20));
        make.width.mas_equalTo(scrWidth());
        make.height.mas_equalTo(screenScale(1));
    }];
    
    return xView;
}
//自定义section头部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return screenScale(68);
}

//自定义section的尾部
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *xView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, screenScale(67))];
    xView.backgroundColor=[UIColor whiteColor];
    
    UIView *backLineView2=[[UIView alloc]initWithFrame:CGRectMake(screenScale(20), (0), DEF_SCREEN_WIDTH-screenScale(20), screenScale(1))];
    backLineView2.backgroundColor=[ZJCModel colorWithString:@"cacaca"];
    [xView addSubview:backLineView2];
    /*
    //发货方式
    UILabel *fLable=[[UILabel alloc]initWithFrame:CGRectMake((10), (13), (80), (14))];
    fLable.text=@"发货方式";
    [xView addSubview:fLable];
    
    //快递
    UILabel *kLable=[[UILabel alloc]initWithFrame:CGRectMake(DEF_SCREEN_WIDTH-(150), (13), (140), (14))];
    kLable.text=@"快递：（包邮）";
    kLable.textAlignment=NSTextAlignmentRight;
    [xView addSubview:kLable];
   
    //线3
    UIView *backLineView3=[[UIView alloc]initWithFrame:CGRectMake((10), (40), DEF_SCREEN_WIDTH-(20), (1))];
    backLineView3.backgroundColor=[ZJCModel colorWithString:@"f2f2f2"];
    [xView addSubview:backLineView3];
    */
    //订单
    UILabel *dLable=[[UILabel alloc]initWithFrame:CGRectMake(screenScale(20), 0, screenScale(120), screenScale(67))];
    dLable.text = @"订单总计";
    dLable.font = get4sFont(12);
    [xView addSubview:dLable];
    
    //价格
    float price = 0.00;
    for (int i = 0; i < [[orderArr[section] objectForKey:@"ordershop"]count] ; i++) {
        
        price = price + [[[orderArr[section] objectForKey:@"ordershop"][i] objectForKey:@"price"]floatValue] * [[[orderArr[section] objectForKey:@"ordershop"][i] objectForKey:@"num"]floatValue];
    }
    //all_money += price;
    
    UILabel *pLable=[[UILabel alloc]init];
    pLable.text = [NSString stringWithFormat:@"%.2f",price];
    pLable.font = [UIFont systemFontOfSize:(14)];
    pLable.textAlignment=NSTextAlignmentRight;
    [xView addSubview:pLable];
    [pLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(xView).with.offset(0);
        make.right.mas_equalTo(xView).with.offset(screenScale(-30));
        
        make.height.mas_equalTo(screenScale(67));
    }];
    
    return xView;
}

//自定义section尾部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return screenScale(67);
}


#pragma mark -----UITableViewDelegate-----
//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return screenScale(135);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    orderTableViewCell *cell=(orderTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"orderTableViewCell"];
    if (cell == nil) {
        cell=[[orderTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"orderTableViewCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    //    cell.settingVC=self;//控制器传给cell
    
    NSString *urlWare=[[orderArr[indexPath.section] objectForKey:@"ordershop"][indexPath.row] objectForKey:@"shoppic"];
    NSString *urlN=manage(AP_HOST, urlWare);
    NSString *strUrl = [urlN stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
    NSURL *urlW=[NSURL URLWithString:strUrl];
    [cell.shouImageView sd_setImageWithURL:urlW placeholderImage:[UIImage imageNamed:@"niunai"]];
    
    cell.nameLable.text = [[orderArr[indexPath.section] objectForKey:@"ordershop"][indexPath.row] objectForKey:@"shop_name"];
    
    NSString *priceStr = @"¥";
    cell.priceLable.text = [priceStr stringByAppendingString: [[orderArr[indexPath.section] objectForKey:@"ordershop"][indexPath.row] objectForKey:@"price"]];
    
    NSString *numStr = @"x";
    cell.numLable.text = [numStr stringByAppendingString: [[orderArr[indexPath.section] objectForKey:@"ordershop"][indexPath.row] objectForKey:@"num"]];
    
    return cell;
}

-(void)xButtonClick{
    NSLog(@"收件人");
    addressViewController *addressVC=[[addressViewController alloc]init];
    [self.navigationController pushViewController:addressVC animated:YES];
}

@end
